package com.mediacloud.app.newsmodule.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SPUtils;
import com.chinamcloud.project.shanshipin.listadpter.SpaceItem;
import com.eventTrack.TXReportManager;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.mediacloud.app.assembly.clickutils.NoDoubleClickUtils;
import com.mediacloud.app.model.activity.BaseBackActivity;
import com.mediacloud.app.model.interfaces.PermissionUtil;
import com.mediacloud.app.model.utils.WekaupManager;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.adaptor.ai.AiSettingAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Date:2022/7/9
 * Author:kgf
 */
public class AIAssistantSettingActivity extends BaseBackActivity implements CompoundButton.OnCheckedChangeListener {


    private View topView;

    private RecyclerView recyclerView;

    private AiSettingAdapter mAdapter;

    private Switch superplayer_switch;

    private ArrayList<Integer> list;


    @Override
    public int getLayoutResID() {
        return R.layout.activity_ai_assistant_setting_layout;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        dataList = new ArrayList<>();
        setTitle("AI助手设置");
        list = new ArrayList<>();
        list.add(R.mipmap.voice_ai_bg1);
        list.add(R.mipmap.voice_ai_bg2);
        list.add(R.mipmap.voice_ai_bg3);
        list.add(R.mipmap.voice_ai_bg4);
        initView();
    }

    private void initView() {
        topView = findViewById(R.id.topview);
        recyclerView = findViewById(R.id.recyclerView);
        superplayer_switch = findViewById(R.id.superplayer_switch_accelerate);
        superplayer_switch.setOnCheckedChangeListener(this);
        mAdapter = new AiSettingAdapter(AIAssistantSettingActivity.this, list);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        recyclerView.addItemDecoration(new SpaceItem(getResources().getDimensionPixelSize(R.dimen.dimen5)));
        recyclerView.setAdapter(mAdapter);
        superplayer_switch.setChecked(SPUtils.getInstance().getBoolean("wekaup_switch", false));
    }

    @Override
    protected String getContent_show_top_color() {
        topView.setBackgroundColor(Color.WHITE);
        return "#333333";
    }

    @Override
    public int getStatusBarColor() {
        return Color.WHITE;
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            XXPermissions.with(this)
                    .permission(Permission.RECORD_AUDIO)
                    .request(new OnPermissionCallback() {
                        @Override
                        public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                            checkAiSwitch(true);
                        }

                        @Override
                        public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
                            checkAiSwitch(false);
                            superplayer_switch.setChecked(false);
                            //是否被永久拒绝
                            if (doNotAskAgain) {
                                PermissionUtil.INSTANCE.showPermissionPop(AIAssistantSettingActivity.this,
                                        "提示",
                                        "当前没有录音或麦克风权限，请在设置中打开录音或麦克风权限",
                                        null);
                            }
                        }
                    });

        } else {
            checkAiSwitch(false);
        }
    }

    private void checkAiSwitch(boolean isChecked) {
        if (NoDoubleClickUtils.isDoubleClick()) {
            return;
        }
        SPUtils.getInstance().put("wekaup_switch", isChecked);
        String checkOpen;
        if (isChecked) {
            WekaupManager.getInstance().setContextInit(this);
            checkOpen = "开";
        } else {
            if (WekaupManager.getInstance().isWekauping()) {
                WekaupManager.getInstance().stopWekaup();
            }
            checkOpen = "关";
        }
        TXReportManager.getInstance().app_jhapp_my_settings_manage("修改AI语音唤醒",checkOpen, "成功", "");
    }
}
